/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/windows/nhRegistry.cpp,v 1.6 2003/11/21 16:12:58 itsme Exp $
 *
 *
 *  this module is a wrapper around the winapi registry functions
 *
 */

#include <windows.h>

#include "tchar.h"
#include "debug.h"

#include "nhRegistry.h"

bool CreateRegistryPath(HKEY hive, const TCHAR *path, HKEY& hkey)
{
    DWORD disp;
    // doc says it needs a 'class' string, everybody else says class don't exist.
    LONG res= RegCreateKeyEx(hive, path, 0, L"??", 0, KEY_WRITE, NULL, &hkey, &disp);
    if (res!=ERROR_SUCCESS)
    {
        error(res, "RegCreateKeyEx");
        return false;
    }

    return true;
}
bool SetRegistryString(HKEY hive, const TCHAR *path, const TCHAR *name, const TCHAR *value)
{
    HKEY hk;
    if (!CreateRegistryPath(hive, path, hk))
        return false;
    LONG res= RegSetValueEx(hk, name, 0, REG_SZ, (BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
    if (res!=ERROR_SUCCESS)
    {
        error(res, "RegSetValueEx");
        return false;
    }
    RegCloseKey(hk);

    return true;
}
bool SetRegistryBoolean(HKEY hive, const TCHAR *path, const TCHAR *name, bool bValue)
{
    HKEY hk;
    if (!CreateRegistryPath(hive, path, hk))
        return false;
    DWORD value = bValue ? 1 : 0;
    LONG res= RegSetValueEx(hk, name, 0, REG_DWORD, (BYTE*)&value, sizeof(DWORD));
    if (res!=ERROR_SUCCESS)
    {
        error(res, "RegSetValueEx");
        return false;
    }
    RegCloseKey(hk);

    return true;
}
bool SetRegistryDword(HKEY hive, const TCHAR *path, const TCHAR *name, DWORD value)
{
    HKEY hk;
    if (!CreateRegistryPath(hive, path, hk))
        return false;
    LONG res= RegSetValueEx(hk, name, 0, REG_DWORD, (BYTE*)&value, sizeof(DWORD));
    if (res!=ERROR_SUCCESS)
    {
        error(res, "RegSetValueEx");
        return false;
    }
    RegCloseKey(hk);

    return true;
}

bool GetRegistryString(HKEY hive, const TCHAR *path, const TCHAR *name, TCHAR *strbuf, int maxlen)
{
    HKEY hk;
    // doc says it needs a 'class' string, everybody else says class don't exist.
    LONG res= RegOpenKeyEx(hive, path, 0, KEY_QUERY_VALUE, &hk);
    if (res!=ERROR_SUCCESS)
    {
        error(res, "GetRegistryString - RegOpenKeyEx(%08lx, %ls)", hive, path);
        return false;
    }

    DWORD valtype;
    DWORD vallen= maxlen;
    res= RegQueryValueEx(hk, name, 0, &valtype, (BYTE*)strbuf, &vallen);
    if (res!=ERROR_SUCCESS)
    {
        error(res, "GetRegistryString - RegQueryValueEx(%08lx, %ls, %ls)", hive, path, name);
        return false;
    }
    RegCloseKey(hk);
    if (valtype!=REG_SZ)
    {
        debug("ERROR: invalid registry key type\n");
        return false;
    }
    strbuf[maxlen-1]=0;

    return true;
}

bool GetRegistryBoolean(HKEY hive, const TCHAR *path, const TCHAR *name, bool *pBool)
{
    HKEY hk;
    // doc says it needs a 'class' string, everybody else says class don't exist.
    LONG res= RegOpenKeyEx(hive, path, 0, KEY_QUERY_VALUE, &hk);
    if (res!=ERROR_SUCCESS)
    {
        error(res, "GetRegistryBoolean - RegOpenKeyEx(%08lx, %ls)", hive, path);
        return false;
    }

    DWORD value;
    DWORD valtype;
    DWORD vallen= sizeof(DWORD);
    res= RegQueryValueEx(hk, name, 0, &valtype, (BYTE*)&value, &vallen);
    if (res!=ERROR_SUCCESS)
    {
        error(res, "GetRegistryBoolean - RegQueryValueEx(%08lx, %ls, %ls)", hive, path, name);
        return false;
    }
    RegCloseKey(hk);
    if (valtype!=REG_DWORD)
    {
        debug("ERROR: invalid registry key type\n");
        return false;
    }
    *pBool= (value!=0);

    return true;
}
bool GetRegistryDword(HKEY hive, const TCHAR *path, const TCHAR *name, DWORD *pValue)
{
    HKEY hk;
    // doc says it needs a 'class' string, everybody else says class don't exist.
    LONG res= RegOpenKeyEx(hive, path, 0, KEY_QUERY_VALUE, &hk);
    if (res!=ERROR_SUCCESS)
    {
        error(res, "GetRegistryDword - RegOpenKeyEx(%08lx, %ls)", hive, path);
        return false;
    }

    DWORD value;
    DWORD valtype;
    DWORD vallen= sizeof(DWORD);
    res= RegQueryValueEx(hk, name, 0, &valtype, (BYTE*)&value, &vallen);
    if (res!=ERROR_SUCCESS)
    {
		//Frank
       // error(res, "GetRegistryDword - RegQueryValueEx(%08lx, %ls, %ls)", hive, path, name);
        return false;
    }
    RegCloseKey(hk);
    if (valtype!=REG_DWORD)
    {
        debug("ERROR: invalid registry key type\n");
        return false;
    }
    *pValue= value;

    return true;
}



